package com.snow.useviewdemo.livedata;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

/**
 * Created by zhaoyt on 1/31/23.
 * Describe:
 */
public class MyViewModel extends ViewModel {
    //声明一个变量 MutableLiveData是容器 Integer是变量的类型
    private MutableLiveData<Integer> LikedNumber;

    public MutableLiveData<Integer> getLikedNumber() {
        if (LikedNumber == null) {
            //LikedNumber是对象类型，不是基本数据类型，所以要保证变量不是空的
            LikedNumber = new MutableLiveData<>();
            //初始化为0
            LikedNumber.setValue(0);
        }
        return LikedNumber;
    }

    public void addLikedNumber(int n) {
        LikedNumber.setValue(LikedNumber.getValue() + n);
    }
}
